#include <cstdio>
#include <dirent.h>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
#include <errno.h>
#include <map>
using namespace std;

int main(int argc, char** argv) {
	char* dir = argv[1];
	DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir)) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

	vector<string> pipes;	
	map<string,FILE*> fdes;
    while ((dirp = readdir(dp)) != NULL) {
		string nm = string(dirp->d_name);
		if(nm != "." && nm != "..")
	        pipes.push_back(string(nm));
		//printf("%s\n", pipes.back().c_str());
		fdes[nm] = fopen((string(argv[1]) + "/" + nm).c_str(), "w");
    }
    closedir(dp);

	char id[1000];
	char buf[1000];
	
	printf("CZEKAM NA POLECENIA\n");
	while(1) {
		scanf("%s", id);
		fgets(buf,1000,stdin);
	int a = 	fprintf(fdes[string(id)], "%s", buf);
			fflush(fdes[string(id)]);
	}
	return 0;
}
